package com.jxc.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.DealerGrantRecord;
import com.jxc.model.po.Repertory;
import com.jxc.model.po.Role;
import com.jxc.model.po.User;
import com.jxc.model.vo.DataGrid;
import com.jxc.model.vo.Json;
import com.jxc.model.vo.VDealerGrantRecord;
import com.jxc.model.vo.VUser;
import com.jxc.service.DealerServiceI;
@Service("dealerService")
public class DealerServiceImpl  implements DealerServiceI {
	@Autowired
	private BaseDaoI<User> userdao;
	@Autowired
	private BaseDaoI<DealerGrantRecord> dealerdao;
	@Override
	public DataGrid datagrid(VUser vUser) {
		DataGrid dg = new DataGrid();
		dg.setRows(this.changeModel(this.find(vUser)));
		dg.setTotal(this.total(vUser));
		return dg;
	}
	private Long total(VUser vUser) {
		return null;
	}

	private List<VUser> changeModel(List<User> users) {
		List<VUser> vusers = new ArrayList<VUser>();
		if (users != null && users.size() > 0) {
			for (User user : users) {
				VUser vuser = new VUser();
				BeanUtils.copyProperties(user, vuser);
				if (user.getRole() != null) {
					Role role = user.getRole();
					vuser.setRoid(role.getId());
					vuser.setRoname(role.getName());
					if (user.getRepertory() != null) {
						Repertory repertory = user.getRepertory();
						vuser.setReid(repertory.getId());
						vuser.setRename(repertory.getName());
					}
					vusers.add(vuser);
				}
			}
		}
		return vusers;
	}
	private List<User> find(VUser vUser) {
		String hql = "from user u where u.role.id=1 ";
		List<Object> values = new ArrayList<Object>();
		if (vUser.getSort() != null
				&& vUser.getOrder() != null) {
			hql += " order by " + vUser.getSort() + " "
					+ vUser.getOrder();
		}
		return userdao.find(hql, values, vUser.getPage(),
				vUser.getRows());
	}
	@Override
	public Integer save(VDealerGrantRecord model) {
		try {
			String ids = model.getIds();
			String[] deid = ids.split(",");
			List<Object> values = new ArrayList<Object>();
			Integer result = 0;
			for (int i = 0; i < deid.length; i++) {
				model.setDealerid(Long.valueOf(deid[i]));
				String hql = "from DealerGrantRecord t where t.agency.id="+model.getAgencyid()+" and t.dealer.id="+model.getDealerid();
				List<DealerGrantRecord> list = dealerdao.findList(hql);
				System.out.println(list.size());
				if (list != null && list.size() > 0) {
					result = 1;
				}else{
					DealerGrantRecord DealerGrantRecord = new DealerGrantRecord();
					User agency = new User();
					agency.setId(model.getAgencyid());
					User dealer = new User();
					dealer.setId(model.getDealerid());
					DealerGrantRecord.setAgency(agency);
					DealerGrantRecord.setDealer(dealer);
					DealerGrantRecord.setIsDelete(false);
					DealerGrantRecord.setInitiationTime(new Date());
					dealerdao.save(DealerGrantRecord);
				}
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			
			}
		return null;
	}
	
	public Integer delete(VDealerGrantRecord model,Json j) {
		Integer result = 1;
		List<DealerGrantRecord> list = new ArrayList<DealerGrantRecord>();
		String jMessage = "";

		try {
			String ids = model.getIds();
			String[] idsArray = ids.split(",");
			String daid = "";
			for (int i = 0; i < idsArray.length; i++) {
				model.setDealerid(Long.valueOf(idsArray[i]));
				String hql = "from DealerGrantRecord t where t.agency.id="+model.getAgencyid()+" and t.dealer.id="+model.getDealerid();
			    list = dealerdao.find(hql);
				if (list != null && list.size() > 0) {
					for (DealerGrantRecord dealerGrantRecord: list) {
					    daid += dealerGrantRecord.getId()+",";
					}
				}
			}
		
			for (String id : daid.split(",")) {
				if (!id.trim().equals("0")) {
					DealerGrantRecord agency = dealerdao.get(DealerGrantRecord.class, Long.parseLong(id));
					System.out.println(agency.getId());
					if (agency != null) {
						jMessage += agency.getDealer().getUsername();
						dealerdao.delete(agency);
						jMessage += "关系删除成功! ";
					}
				}
			}
		} catch (Exception e) {
			jMessage += "关系删除失败! ";
			result = 0;
			e.printStackTrace();
		}
		j.setMsg(jMessage);
		return result;
	}
}
